gusucode.com > 支持向量机工具箱 - LIBSVM OSU_SVM LS_SVM源码程序 > 支持向量机工具箱 - LIBSVM OSU_SVM LS_SVM\libsvm\demo4_2.m

    
% DEMO4  Epsilon Support Vector Regression with LIBSVM.
%
% A one-dimensional regression problem is solved by an epsilon-style SVM, i.e.,
% the user has to provide the insensitivity zone in addition to the trade-off
% parameter C. Again, the simulation is done by LIBSVMSIM.

% ------------------------------------------------------------------------------
% MATLAB Interface for LIBSVM, Version 1.2
%
% Copyright (C) 2004-2005 Michael Vogt
% Written by Michael Vogt, Atanas Ayarov and Bennet Gedan
%
% This program is free software; you can redistribute it and/or modify it
% under the terms of the GNU General Public License as published by the Free
% Software Foundation; either version 2 of the License, or (at your option)
% any later version.
% ------------------------------------------------------------------------------

clc
clear
%close all

% ------------------------------------------------------------%
% 定义核函数及相关参数

C = 100;                % 拉格朗日乘子上界
e = 0.2;                % 不敏感损失函数的参数,Epsilon越大,支持向量越少

%ker = struct('type','linear');
%ker = struct('type','ploy','degree',3,'offset',1);
ker = struct('type','gauss','width',1);
%ker = struct('type','tanh','gamma',1,'offset',0);

% ker - 核参数(结构体变量)
% the following fields:
%   type   - linear :  k(x,y) = x'*y
%            poly   :  k(x,y) = (x'*y+c)^d
%            gauss  :  k(x,y) = exp(-0.5*(norm(x-y)/s)^2)
%            tanh   :  k(x,y) = tanh(g*x'*y+c)
%   degree - Degree d of polynomial kernel (positive scalar).
%   offset - Offset c of polynomial and tanh kernel (scalar, negative for tanh).
%   width  - Width s of Gauss kernel (positive scalar).
%   gamma  - Slope g of the tanh kernel (positive scalar).

% ------------------------------------------------------------%
% 构造两类训练样本

n = 50;
rand('state',42);
X  = linspace(-4,4,n)';                           % 训练样本,n×d的矩阵,n为样本个数,d为样本维数,这里d=1
Ys = (1-X+2*X.^2).*exp(-.5*X.^2);
f = 0.2;                                          % 相对误差
Y  = Ys+f*max(abs(Ys))*(2*rand(size(Ys))-1)/2;    % 训练目标,n×1的矩阵,n为样本个数,值为期望输出

figure;
plot(X,Ys,'b-',X,Y,'b*');
title('\epsilon-SVR');
hold on;

% ------------------------------------------------------------%
% 训练支持向量机

tic
svm = libsvmopt(X,Y,C,e,ker);
t_train = toc

% svm  支持向量机(结构体变量)
% the following fields:
%   ker - 核参数
%   x - 训练样本
%   y - 训练目标;
%   a - 拉格朗日乘子

% ------------------------------------------------------------%
% 测试输出

tic
Yd = libsvmsim(svm,X);           % 测试输出
t_sim = toc

plot(X,Yd,'r--',X,[Yd-e,Yd+e],'g:');
hold off;